# -*- coding: utf-8 -*-
import json
import time

import jsonpath
import scrapy


class SmzdmSpider(scrapy.Spider):
    name = 'smzdm'
    allowed_domains = ['smzdm.com']
    time = time.time()
    start_urls = ['https://m.smzdm.com/youhui/ajax_youhui_list?timesort=%s&f=wap' % int(time)]
    page = 1

    def start_requests(self):
        self.headers = {
            "Request-From": "browser",
            "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
        }
        for url in self.start_urls:
            yield scrapy.Request(url, headers=self.headers)

    def parse(self, response):
        # print("第%s页正在爬取" % self.page)
        json_dict = json.loads(response.text)
        # print(json_dict)
        json_count = jsonpath.jsonpath(json_dict, "$..rows")
        json_count_for = json_dict["data"]["rows"]

        for json_temp in json_count:
            for json_str in json_temp:
                print(json_temp)
                yield {
                    'title': json_str['article_title'],
                    'price': json_str['article_price'],
                    'date': json_str['article_format_date'],
                    'mall': json_str['article_mall']
                }

        for id, json_temp in enumerate(json_count_for):
            if id == 19 and self.page < 50:
                self.page += 1
                time_sort = json_temp.get("time_sort")
                url = 'https://m.smzdm.com/youhui/ajax_youhui_list?timesort=%s&f=wap' % time_sort
                yield scrapy.Request(url=url, headers=self.headers, callback=self.parse)
